#pragma once
#include <string>
#include "XnccServer.h"
#include "XnccType.h"
#include "flat_hash_map"
namespace xncc {
namespace foundation {
    class EventLoop;
    class StreamSocketChannel;
    class SubStreamServer
    {
      public:
        explicit SubStreamServer(EventLoop* loop);
        ~SubStreamServer();

        SubStreamServer(const SubStreamServer&)             = delete;
        SubStreamServer(const SubStreamServer&&)            = delete;
        SubStreamServer& operator=(const SubStreamServer&)  = delete;
        SubStreamServer& operator=(const SubStreamServer&&) = delete;

        [[nodiscard]] auto getLoop() const -> EventLoop* { return loop_; }
        void               remove(types::connection_id_t connId) { clientChs_.erase(connId); }

        void acceptNewConnection(int sockfd, std::shared_ptr<XnccAddress> peerAddr);

      private:
        void       newConnection(int sockfd, std::shared_ptr<XnccAddress> peerAddr);
        void       removeConnection(types::connection_id_t connId, const std::string& disconnectedReason);
        void       handleIoMsg(const char* msg, uint32_t len, types::thread_id_t tid, types::connection_id_t connId);
        EventLoop* loop_;  //
        // 这个设计是一个server使用一个线程
        ska::flat_hash_map<types::connection_id_t, std::shared_ptr<StreamSocketChannel>> clientChs_;
    };
}  // namespace foundation
}  // namespace xncc